
let mData  = null
let mConfs = null

module.exports = {

    setup()
    {
        let notchstr = null

        const p = require('ConstBase')
        
        mData = {}
 
        if (cc.sys.isNative)
        {
            if (cc.sys.os == cc.sys.OS_ANDROID)
            {
                mData[p.device_uid.key]  = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "deviceIdentifying", "()Ljava/lang/String;")
                mData[p.device_name.key] = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "phoneName", "()Ljava/lang/String;")
                mData[p.country.key]     = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "country", "()Ljava/lang/String;")
                mData[p.os_version.key]  = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "phoneOsVersion", "()Ljava/lang/String;")

                mData[p.channel.key]     = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "metaDataByApplication", "(Ljava/lang/String;)Ljava/lang/String;", 'OGRE_CHANNEL')
                notchstr = jsb.reflection.callStaticMethod("com/mi/AndroidBridge", "notchSize", "()Ljava/lang/String;")
            }
            else if (cc.sys.os == cc.sys.OS_IOS)
            {
                mData[p.device_uid.key]  = jsb.reflection.callStaticMethod("AppleBridge", "deviceId")
                mData[p.device_name.key] = jsb.reflection.callStaticMethod("AppleBridge", "deviceName")//machineModel
                mData[p.country.key]     = jsb.reflection.callStaticMethod("AppleBridge", "country")
                mData[p.os_version.key]  = jsb.reflection.callStaticMethod("AppleBridge", "osVersion")
                mData[p.channel.key]     = jsb.reflection.callStaticMethod("AppleBridge", "metaDataByApplication:", 'OGRE_CHANNEL')
            }
        }
        else
        {
            let uid = ''
            if (window.location.href.indexOf('?') != -1)
            {
                let arr = window.location.href.split('?')
                uid = arr[1]
            }
            else 
            {
                uid = cc.sys.localStorage.getItem('deviceId')
                if (!uid)
                {
                    let S4 = () =>
                    {
                        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                    }
                    
                    let guid = ()=>
                    {
                        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
                    }

                    uid = guid()
                    cc.sys.localStorage.setItem('deviceId', uid)
                }
            }

            mData[p.device_uid.key] = uid
            mData[p.os_version.key] = cc.sys.browserVersion
        }


        mConfs = require('Channels')[this.get(p.channel)]

        let lan = mConfs.language
        if (!lan)
        {
            let ss = require('StorageSystem').share()
            lan = ss.get(p.storge_language)

            if (!lan)
            {
                lan = cc.sys.language
            }
        }
        else
        {
            mData[p.fix_language.key] = lan
        }
        mData[p.language.key] = lan

        Log.i("app info: " + JSON.stringify(mData))
    },

    get(p)
    {
        if (mData)
        {
            return mData[p.key] || p.def
        }

        return p.def
    },

    set(p, v)
    {
        if (mData)
        {
            mData[p.key] = v
        }
    },

    platform()
    {
        if (cc.sys.os == cc.sys.OS_ANDROID)
        {
            return cc.sys.ANDROID
        }
        else if (cc.sys.os == cc.sys.OS_IOS)
        {
            return cc.sys.IPHONE
        }
        return cc.sys.WIN32
    },

    configBy(k)
    {
        if (mConfs)
        {
            return mConfs[k]
        }
    },

    channel()
    {
        const p = require('ConstBase')
        return mData[p.channel.key] || p.channel.def
    },

    enableAdvertisement()
    {
        return mConfs.advertisement
    }
}
